<?php

class GeTui
{
    private $host = 'http://sdk.open.api.igexin.com/apiex.htm';
    //测试
    private $appkey = 'tAPSS21Aui9meLKq7wJIz3';
    private $appid = 'ZmD1uGOxua8p62jNOA0gA9';
    private $mastersecret = 'UkCBUGiEMp57D9Wg5RW9z1';



    private function init($appid=null,$appkey=null,$mastersecret=null)
    {
        $this->appid = !empty($appid) ? $appid : "ZmD1uGOxua8p62jNOA0gA9";
        $this->appkey = !empty($appkey) ? $appkey : "tAPSS21Aui9meLKq7wJIz3";
        $this->mastersecret = !empty($mastersecret) ? $mastersecret : "UkCBUGiEMp57D9Wg5RW9z1";
        $this->host = "http://sdk.open.api.igexin.com/apiex.htm";
    }

    public function __construct($appid=null,$appkey=null,$mastersecret=null)
    {
        $this->init($appid,$appkey,$mastersecret);
        $this->__loader();
    }

    private function __loader()
    {
        require_once(dirname(__FILE__) . '/' . 'IGt.Push.php');
        require_once(dirname(__FILE__) . '/' . 'igetui/IGt.AppMessage.php');
        require_once(dirname(__FILE__) . '/' . 'igetui/IGt.TagMessage.php');
        require_once(dirname(__FILE__) . '/' . 'igetui/IGt.APNPayload.php');
        require_once(dirname(__FILE__) . '/' . 'igetui/template/IGt.BaseTemplate.php');
        require_once(dirname(__FILE__) . '/' . 'IGt.Batch.php');
        require_once(dirname(__FILE__) . '/' . 'igetui/utils/AppConditions.php');
        require_once(dirname(__FILE__) . '/' . 'igetui/template/notify/IGt.Notify.php');
        require_once(dirname(__FILE__) . '/' . 'igetui/IGt.MultiMedia.php');
        require_once(dirname(__FILE__) . '/' . 'payload/VOIPPayload.php');
    }

    //单推
    public function pushMessageToSingle($cid){
        $igt = new \IGeTui($this->host,$this->appkey,$this->mastersecret);

        // 透传消息模板
        $template = $this->IGtTransmissionTemplateDemo();

        //定义"SingleMessage"
        $message = new \IGtSingleMessage();

        $message->set_isOffline(false);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送

        //接收方
        $target = new \IGtTarget();
        $target->set_appId($this->appid);
        $target->set_clientId($cid);

        try {
            $rep = $igt->pushMessageToSingle($message, $target);
            var_dump($rep);
            echo ("<br><br>");

        }catch(RequestException $e){
            $requstId =e.getRequestId();
            //失败时重发
            $rep = $igt->pushMessageToSingle($message, $target,$requstId);
            var_dump($rep);
            echo ("<br><br>");
        }
    }

    //透传消息模板
    public function IGtTransmissionTemplateDemo(){
        $listId = [
            'title' => '通知',
            'content' => '你有一条新消息',
            'payload' => [
                "push"=> "inner",
                "event"=> "warning",
                "silent"=> false,
            ]
        ];
        $mes = [
            'title' => '通知',
            'content' => '你有一条新消息',
            'payload' => [
                "push"=> "inner",   
                "event"=> "warning",
                "silent"=> false,
                "data"=> ""
            ]
        ];
        $template =  new \IGtTransmissionTemplate();

        $template->set_appId($this -> appid);//应用appid
        $template->set_appkey($this->appkey);//应用appkey
        $template->set_transmissionType(1);//透传消息类型
        $template->set_transmissionContent(json_encode($listId));//透传内容

        //注意：如果设备离线（安卓），一定要设置厂商推送，不然接收不到推送（比如华为、小米等等）
        //S.title=的值为推送消息标题，对应5+ API中PushMessage对象的title属性值；
        //S.content=的值为推送消息内容，对应5+ API中PushMessage对象的content属性值；
        //S.payload=的值为推送消息的数据，对应5+ API中PushMessage对象的payload属性值；
        $intent = 'intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=此处为打包APP的包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=标题;S.content=内容;S.payload=数据;end';

        $notify = new \IGtNotify();
        $notify->set_title('通知');
        $notify->set_content('你有一条新消息');
        $notify->set_intent($intent);
        $notify->set_type(NotifyInfo_type::_intent);
        $template->set3rdNotifyInfo($notify);

        //下面这些是苹果需要设置的，只要是ios系统的，都要设置这个，不然离线收不到
        //APN高级推送
        $alertmsg=new \DictionaryAlertMsg();
        $alertmsg->body=$mes['content'];
        $alertmsg->actionLocKey="查看";
        $alertmsg->locKey=$listId['content'];
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
//        IOS8.2 支持
        $alertmsg->title=$mes['title'];
        $alertmsg->titleLocKey="测试";
        $alertmsg->titleLocArgs=array("TitleLocArg");

        $apn = new \IGtAPNPayload();
        $apn->alertMsg=$alertmsg;
        $apn->badge=0;
        $apn->sound="";
        $apn->add_customMsg("payload","payload");
        $apn->contentAvailable=0;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);

        return $template;
    }


}